package com.wbao.dianniu.ui;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.Mode;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshBase.OnRefreshListener;
import com.alibaba.mobileim.fundamental.widget.refreshlist.PullToRefreshListView;
import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.ActivityInListAdapter;
import com.wbao.dianniu.data.ActivityInListData;
import com.wbao.dianniu.listener.IActivityInListListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.ActivityInListManager;
import com.wbao.dianniu.utils.Utils;
import com.wbao.dianniu.R;

/**
 * 报名管理
 * 
 * @author yejianjun
 *
 */
public class SignUpManagerActivity extends Activity implements IActivityInListListener,OnClickListener {

	private PullToRefreshListView pullListView;
	private ListView listView;
	private final int PAGE_COUNTS = 10;
	private int currentPage = 0;
	private TextView noDataTV;
	private ActivityInListManager manager;
	private ActivityInListAdapter adapter;
	private List<ActivityInListData> mList = new ArrayList<>();
	private int id;
	private TextView chooseTV;
	private ImageButton backBtn;
	private PopupWindow popupwindow; 
	private final Integer TYPE_ALL = null;
	private final Integer TYPE_PENDING = 1;
	private final Integer TYPE_AGREED = 2;
	private final Integer TYPE_REFUSED = 3;
	private Integer status = TYPE_ALL;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			Utils.setTranslucentStatus(this,true);
			SystemBarTintManager tintManager = new SystemBarTintManager(this);
			tintManager.setStatusBarTintEnabled(true);
			tintManager.setStatusBarTintResource(R.color.title_bg);// 通知栏所需颜色
		}
		setContentView(R.layout.activity_signup_manager);
		initView();
		initData();
		 initmPopupWindowView();
	}
	
	private void initView() {
		noDataTV = (TextView) findViewById(R.id.no_find_data);
		pullListView = (PullToRefreshListView) findViewById(R.id.signup_manager_listview);
		pullListView.setMode(Mode.PULL_UP_TO_REFRESH);
		pullListView.setOnRefreshListener(onRefreshListener);
		listView = pullListView.getRefreshableView();
		registerForContextMenu(listView);
		
		backBtn = (ImageButton) findViewById(R.id.signup_back_button);
		chooseTV = (TextView) findViewById(R.id.choose_status_tv);
		backBtn.setOnClickListener(this);
		chooseTV.setOnClickListener(this);
	}

	private void initData() {
		manager = new ActivityInListManager(this);
		manager.addActivityInListListener(this);
		adapter = new ActivityInListAdapter(this);
		adapter.addData(mList);
		listView.setAdapter(adapter);
		id = getIntent().getIntExtra(GlobalContext.BUNDLE_ID,0);
		reqData(currentPage ,status);
	}
	OnRefreshListener onRefreshListener = new OnRefreshListener() {

		@Override
		public void onRefresh() {
			// 上拉刷新 加载请求数据
			pullUpRequest();
		}
	};

	// 上拉刷新数据请求
	private void pullUpRequest() {
		// 没有搜索结果 或者数据为空 上拉没有效果
		currentPage++;
		reqData(currentPage,status);
	}
	private void reqData(int page,Integer type) {
		manager.activityInList(GlobalContext.accountId, id, type, page * PAGE_COUNTS, PAGE_COUNTS);
	}

	@Override
	public void onActivityInListFailure(int errorCode, String errorMsg) {
		// TODO Auto-generated method stub
		pullListView.onRefreshComplete(false, false);
	}

	@Override
	public void onActivityInListSuccess(List<ActivityInListData> list) {
		pullListView.onRefreshComplete(false, false);
		if (null != list) {
			adapter.addData(list);
			mList = adapter.getList();
			adapter.notifyDataSetChanged();
			if (mList.size() == 0) {
				noDataTV.setVisibility(View.VISIBLE);
			} else {
				noDataTV.setVisibility(View.GONE);
			}
		}
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.choose_status_tv:
			if (popupwindow != null&&popupwindow.isShowing()) {  
                popupwindow.dismiss();  
                return;  
            } else {
                popupwindow.showAsDropDown(v, 0, 0);
            }  
			break;
		case R.id.signup_back_button:
			finish();
			break;
		case R.id.signup_pop_all:
			if (popupwindow != null && popupwindow.isShowing()) {  
                popupwindow.dismiss();  
            } 
			chooseTV.setText("全部");
			adapter.clear();
			currentPage = 0;
			status = TYPE_ALL;
			reqData(currentPage, status);
			break;
		case R.id.signup_pop_pending:
			if (popupwindow != null && popupwindow.isShowing()) {  
                popupwindow.dismiss();  
            }  
			chooseTV.setText("待处理");
			adapter.clear();
			currentPage = 0;
			status = TYPE_PENDING;
			reqData(currentPage, status);
			break;
		case R.id.signup_pop_agreed:
			if (popupwindow != null && popupwindow.isShowing()) {  
                popupwindow.dismiss();  
            }  
			chooseTV.setText("已同意");
			adapter.clear();
			currentPage = 0;
			status = TYPE_AGREED;
			reqData(currentPage, status);
			break;
		case R.id.signup_pop_refused:
			if (popupwindow != null && popupwindow.isShowing()) {  
                popupwindow.dismiss();  
            }
			chooseTV.setText("已拒绝");
			adapter.clear();
			currentPage = 0;
			status = TYPE_REFUSED;
			reqData(currentPage, status);
			break;
		default:
			break;
		}
	}
	
	public void initmPopupWindowView() {  
        // // 获取自定义布局文件pop.xml的视图  
        View customView = getLayoutInflater().inflate(R.layout.signup_popview, null, false);  
        // 创建PopupWindow实例,200,150分别是宽度和高度  
        popupwindow = new PopupWindow(customView, Utils.dip2px(this, 120), LinearLayout.LayoutParams.WRAP_CONTENT);  
        popupwindow.setOutsideTouchable(true);
        customView.setOnTouchListener(new OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (popupwindow != null && popupwindow.isShowing()) {  
                    popupwindow.dismiss();  
                }  
				return false;
			}
		});
        /** 在这里可以实现自定义视图的功能 */  
        Button btton1 = (Button) customView.findViewById(R.id.signup_pop_all);  
        Button btton2 = (Button) customView.findViewById(R.id.signup_pop_pending);  
        Button btton3 = (Button) customView.findViewById(R.id.signup_pop_agreed);  
        Button btton4 = (Button) customView.findViewById(R.id.signup_pop_refused);
        btton1.setOnClickListener(this);
        btton2.setOnClickListener(this);  
        btton3.setOnClickListener(this);  
        btton4.setOnClickListener(this);  
    } 
	
	@Override
	protected void onDestroy() {
		if (popupwindow != null && popupwindow.isShowing()) {  
            popupwindow.dismiss();  
            popupwindow = null; 
        }  
		super.onDestroy();
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
	}
}
